﻿using DevExpress.CrmDemo.Win.Data;
using DevExpress.CrmDemo.Win.Helpers;
using System.Linq;
using System.Windows.Forms;
using System;
using System.IO;

namespace DevExpress.CrmDemo.Win {
    public partial class ActivityEditorForm : DevExpress.XtraEditors.XtraForm {
        #region Constructor
        public ActivityEditorForm() {
            InitializeComponent();
            labelControl10.ForeColor = ColorHelper.LabelColorLight;
            labelControl1.ForeColor = ColorHelper.LabelColorLight;
            labelControl2.ForeColor = ColorHelper.LabelColorLight;
            labelControl4.ForeColor = ColorHelper.LabelColorLight;
            labelControl7.ForeColor = ColorHelper.LabelColorLight;
            ColorHelper.SetMenuManager( Controls, this.LookAndFeel.ActiveSkinName, this);
        }

        public ActivityEditorForm(Activity activity)
            : this() {
            this.activityBindingSource.DataSource = activity;

            DataBindingHelpers.SuspendTwoWayBinding(this.BindingContext[activityBindingSource]);
        }
        #endregion

        public event NavRequestEventHandler NavRequest;

        protected void OnNavRequest(NavRequestEventArgs args) {
            if (NavRequest != null)
                NavRequest(this, args);
        }

        #region Event Handlers

        private void simpleButtonClose_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void simpleButtonSave_Click(object sender, EventArgs e) {
            DataBindingHelpers.UpdateDataBoundObject(this.BindingContext[activityBindingSource], activityBindingSource);

            if (this.Validate()) {
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void OpportunitiesEditorForm_Load(object sender, System.EventArgs e) {
            this.teamMemberBindingSource.DataSource = DataProvider.Instance.TeamMembers;
        }

        private void buttonEditFilePath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (string.IsNullOrEmpty(buttonEditFilePath.Text))
                openFileDialog.InitialDirectory = "c:\\";
            else
                openFileDialog.InitialDirectory = Path.GetDirectoryName(buttonEditFilePath.Text);

            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                buttonEditFilePath.Text = openFileDialog.FileName;
            }
        }

        #endregion
    }
}
